﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Web.Razor;
using System.IO;

namespace RazorTemplateEngine
{
    public class RazorToCompileUnitParser
    {
        public CodeCompileUnit Parse(ParserRequestContext request)
        {
            if (request == null) throw new ArgumentNullException("request");
            TemplatePageRazorHost host = new TemplatePageRazorHost(new CSharpRazorCodeLanguage());
            //support customize base class
            host.DefaultBaseClass = TemplateDriver.DefaultTemplateBaseType.FullName;
            System.Web.Razor.RazorTemplateEngine rte = new System.Web.Razor.RazorTemplateEngine(host);
            CodeCompileUnit templdateUnit = rte.GenerateCode(new StringReader(request.TemplateSource)).GeneratedCode;
            return templdateUnit;
        }
    }
}
